package mozi.com.oneday;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import mozi.com.oneday.gson.User;
import mozi.com.oneday.utils.HttpUtil;
import mozi.com.oneday.utils.Utility;
import okhttp3.Call;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {


    private static final String TAG = "RegisterActivity";

    @Bind(R.id.textView2)
    TextView textView2;
    @Bind(R.id.textView3)
    TextView textView3;
    @Bind(R.id.login_progress)
    ProgressBar loginProgress;
    @Bind(R.id.register_button)
    Button registerButton;
    @Bind(R.id.editText)
    EditText editText;
    @Bind(R.id.textInputLayoutEmail)
    TextInputLayout textInputLayoutEmail;
    @Bind(R.id.editText2)
    EditText editText2;
    @Bind(R.id.textInputLayoutPassword)
    TextInputLayout textInputLayoutPassword;
    @Bind(R.id.editText3)
    EditText editText3;
    @Bind(R.id.textInputLayoutName)
    TextInputLayout textInputLayoutName;
    @Bind(R.id.is_as_text)
    TextView isAsText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.register_button)
    public void onViewClicked() {
        sendRequestWithOkHttp();
    }



    private void sendRequestWithOkHttp(){

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String userUrl="http://47.93.0.157:8080/OneDay/user?email=903328615@qq.com";
                HttpUtil.sendOkHttpRequest(userUrl,new okhttp3.Callback(){

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String responseText =response.body().string();
                        User user= Utility.handleWeatherResponse(responseText);
                        Log.d(TAG, "onResponse: "+user.toString());
                        Log.d(TAG, "onResponse: "+user.getExtend().getUser().getPassword());
                        Log.d(TAG, "onResponse: "+responseText);
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(RegisterActivity.this,"获取用户数据失败",Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                });

            }
        });


    }
}
